<?php

namespace Xin\Coze\Providers;

use Xin\Coze\SteamIterator;

class WorkflowProvider extends AbstractProvider
{
	/**
	 * 运行工作流
	 * @param string $workflowId
	 * @param array $parameters
	 * @param array{
	 *     chat_id?: string,
	 *     ext?: array<string,mixed>,
	 *     is_async?:bool,
	 *     app_id?:string
	 * } $data
	 * @param array $options
	 * @return array|null
	 */
	public function run(string $workflowId, array $parameters = [], array $data = [], array $options = [])
	{
		return $this->coze->httpPost(
			'/v1/workflow/run',
			array_merge($data, [
				'workflow_id' => $workflowId,
				'parameters' => $parameters ?: new \stdClass(),
			]),
			$options
		);
	}

	/**
	 * 流式运行工作流
	 * @param string $workflowId
	 * @param array $parameters
	 * @param array{
	 *     chat_id?: string,
	 *     ext?: array<string,mixed>,
	 *     app_id?:string
	 * } $data
	 * @param array $options
	 * @return array|null
	 */
	public function streamRun(string $workflowId, array $parameters = [], array $data = [], array $options = [])
	{
		return $this->coze->httpPost(
			'/v1/workflow/stream_run',
			array_merge($data, [
				'workflow_id' => $workflowId,
				'parameters' => $parameters ?: new \stdClass(),
			]),
			$options
		);
	}

	/**
	 * 流式恢复工作流
	 * @param string $workflowId
	 * @param string $eventId
	 * @param int $interruptType
	 * @param string $resumeData
	 * @param array $options
	 * @return array|null
	 */
	public function streamResume(string $workflowId, string $eventId, int $interruptType, string $resumeData, array $options = [])
	{
		return $this->coze->httpPost(
			'/v1/workflow/stream_resume',
			[
				'workflow_id' => $workflowId,
				'event_id' => $eventId,
				'interrupt_type' => $interruptType,
				'resume_data' => $resumeData,
			],
			$options
		);
	}

	/**
	 * 查询工作流异步执行结果
	 * @param string $workflowId
	 * @param string $executeId
	 * @param array $options
	 * @return array|null
	 */
	public function result(string $workflowId, string $executeId, array $options = [])
	{
		return $this->coze->httpGet("/v1/workflows/{$workflowId}/run_histories/{$executeId}", [], $options);
	}

	/**
	 * 运行对话工作流
	 * @param string $workflowId
	 * @param array $parameters
	 * @param array{
	 *     chat_id?: string,
	 *     bot_id?: string,
	 *     conversation_id?: string,
	 *     ext?: array<string,mixed>,
	 *     app_id?:string
	 * } $data
	 * @param array $options
	 * @return array|null
	 */
	public function chat(string $workflowId, array $parameters = [], array $data = [], array $options = [])
	{
		return $this->coze->httpPost(
			'/v1/workflow/chat',
			array_merge($data, [
				'workflow_id' => $workflowId,
				'parameters' => $parameters ?: new \stdClass(),
			]),
			$options
		);
	}
}
